package com.memory.monitor.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.memory.monitor.common.DeleteRequest;
import com.memory.monitor.common.ErrorCode;
import com.memory.monitor.constant.ExcelConstant;
import com.memory.monitor.exception.BusinessException;
import com.memory.monitor.exception.ThrowUtils;
import com.memory.monitor.job.AreaExcelListener;
import com.memory.monitor.job.JobExcelListener;
import com.memory.monitor.job.MarketExcelListener;
import com.memory.monitor.mapper.MarketMapper;
import com.memory.monitor.model.dto.market.MarketAddRequest;
import com.memory.monitor.model.dto.market.MarketQuerryRequest;
import com.memory.monitor.model.dto.market.MarketUpdateRequest;
import com.memory.monitor.model.entity.Job;
import com.memory.monitor.model.entity.Market;
import com.memory.monitor.model.entity.User;
import com.memory.monitor.model.vo.JobVO;
import com.memory.monitor.model.vo.MarketVO;
import com.memory.monitor.service.MarketService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import static com.memory.monitor.constant.ExcelConstant.*;

/**
* @author 21731
* @description 针对表【market(营销情况)】的数据库操作Service实现
* @createDate 2024-09-02 09:59:55
*/
@Service
public class MarketServiceImpl extends ServiceImpl<MarketMapper, Market> implements MarketService {

    /**
     * 封装MarketVO对象
     * @param market
     * @return
     */
    @Override
    public MarketVO getMarketVO(Market market) {
        if (market == null) {
            return null;
        }
        MarketVO marketVO = new MarketVO();
        BeanUtils.copyProperties(market, marketVO);
        return marketVO;
    }

    /**
     * 封装MarketVO对象
     * @param marketList
     * @return
     */
    @Override
    public List<MarketVO> getMarketVO(List<Market> marketList) {
        if (CollectionUtils.isEmpty(marketList)) {
            return new ArrayList<>();
        }
        return marketList.stream().map(this::getMarketVO).collect(Collectors.toList());
    }

    /**
     * 查询采购信息
     * @param marketQuerryRequest
     * @return
     */
    @Override
    public QueryWrapper<Market> getQueryWrapper(MarketQuerryRequest marketQuerryRequest) {
        if (marketQuerryRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "请求参数为空");
        }
        Long id = marketQuerryRequest.getId();
        String productType = marketQuerryRequest.getProductType();
        Date salesDate = marketQuerryRequest.getSalesDate();
        String salesChannel = marketQuerryRequest.getSalesChannel();
        Integer salesNumber = marketQuerryRequest.getSalesNumber();
        Integer saleroom = marketQuerryRequest.getSaleroom();
        String province = marketQuerryRequest.getProvince();
        String city = marketQuerryRequest.getCity();


        QueryWrapper<Market> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(id != null, "id", id);
        queryWrapper.like(StringUtils.isNotBlank(productType), "product_type", productType);
        queryWrapper.eq(salesDate!=null, "sales_ate", salesDate);
        queryWrapper.eq(StringUtils.isNotBlank(salesChannel), "sales_channel", salesChannel);
        queryWrapper.eq(salesNumber!=null, "sales_number", salesNumber);
        queryWrapper.eq(salesNumber!=null, "saleroom", saleroom);
        queryWrapper.eq(StringUtils.isNotBlank(province), "province", province);
        queryWrapper.eq(StringUtils.isNotBlank(city), "city", city);


        return queryWrapper;
    }

    /**
     * 增加采购信息
     * @param marketAddRequest
     * @return
     */
    @Override
    public Long addMarket(MarketAddRequest marketAddRequest) {
        if (marketAddRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Market market = new Market();

        BeanUtils.copyProperties(marketAddRequest, market);
        boolean result = this.save(market);
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
        return market.getId();
    }

    /**
     * 更新采购信息
     * @param marketUpdateRequest
     * @return
     */
    @Override
    public Boolean updateMarket(MarketUpdateRequest marketUpdateRequest) {
        if (marketUpdateRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
       Market market = new Market();
        BeanUtils.copyProperties(marketUpdateRequest,market);
        boolean result = this.updateById(market);
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
        return result;
    }

    /**
     * 删除采购信息
     * @param deleteRequest
     * @return
     */
    @Override
    public Boolean deleteMarket(DeleteRequest deleteRequest) {
        if (deleteRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        boolean result = this.removeById(deleteRequest.getId());
        ThrowUtils.throwIf(result, ErrorCode.OPERATION_ERROR);
        return result;
    }

    @Override
    public List<MarketVO> listMarketByPage() {
        return getMarketVO(this.list());
    }

    @Override
    public Boolean readExcel(MultipartFile multipartFile) {
        // 1.转换文件为字节流
        InputStream inputStream;
        try {
            inputStream = multipartFile.getInputStream();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        // 2.执行文件处理逻辑
        EasyExcel.read(inputStream, Market.class, new MarketExcelListener()).sheet().doRead();
        // 3.入库
        boolean saveBatch = this.saveBatch(MarketExcelListener.CACHED_DATA_LIST);

        ThrowUtils.throwIf(!saveBatch, ErrorCode.PARAMS_ERROR);
        return true;
    }

    @Override
    public Boolean writeExcel(String path) {
        // 1.定义文件路径
        String fullPath = DOWNLOAD_PATH + AREA_TABLE_NAME;
        // 2.检查文件是否存在
        ExcelConstant.isExist(fullPath);
        // 3.根据路径导出
        List<MarketVO> marketList = this.listMarketByPage();
        EasyExcel.write(fullPath, User.class).sheet(AREA_SHEET_NAME).doWrite(marketList);

        return true;
    }
}




